package com.base.param;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.SelectItem;

import java.util.Objects;

@Getter
@Setter
public class SelectItemParam {
    private String columnCode;
    private SqlFunctionKeyword functionName;
    private String alias;

    public SelectItemParam() {
    }

    public SelectItemParam(String columnCode) {
        this.columnCode = columnCode;
    }

    public String getAlias() {
        if (StrUtil.isBlank(alias)) {
            return "'" + columnCode + "'";
        }
        return "'" + alias + "'";
    }

    public SelectItem<?> parseToSelectItem(boolean valueIsPlaceholder) {
        if (Objects.isNull(functionName)) {
            return new SelectItem(new Column(columnCode), new Alias(getAlias()));
        } else {
            return functionName.getExpressionFunction().apply(this, valueIsPlaceholder);
        }
    }
}